
package system.base;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import system.SelectManager;
import system.data.ProductBase;
import framework.core.BizException;
import framework.core.Conditions;
import framework.core.ConstDefine;
import framework.core.DBConnection;
import framework.core.DBEngine;
import framework.core.DBPreparedStatement;
import framework.core.DBResult;
import framework.core.DateUtil;
import framework.core.StringUtil;
import framework.core.Value;


/*
	Author    :chengbiao.he
	CreateTime:2008-01-30 14:21:49
*/
public class Product extends ProductBase 
{
    private String s_mhcx;

//----system generated start----// 	
public Product() 
{
	
}

private static void delete(DBConnection con,String id) throws Exception
{
	Conditions conditions = new Conditions();

	conditions.add(ID,id);
	
	delete(con,TABLE_NAME,conditions);
}
public static Product fetch()
{
	return new Product();
}
public static Product copy(Product srcObj)
{
	Product obj = fetch();
	obj.copyBase(srcObj);
	return obj;
}
public static Product fetch(DBConnection con,String id) throws Exception
{
	Conditions conditions = new Conditions();

	conditions.add(ID,id);
	
	return fetch(con,conditions);
}
/*public static product fetch(DBConnection con,Conditions conditions)throws Exception{
    DBResult result = null;
    try
    {
        
        result = DBEngine.doSelect(con,"select * from " + TABLE_NAME,conditions);
        java.sql.ResultSet rs = result.getRs();
        Product obj = null;
        if(rs.next())
        {
            obj = fetch(rs);
        }
        return obj;
    }
    finally
    {
        if(result!=null) result.clear() ;
    }
    
}
*/

public static Product fetch(DBConnection con,Conditions conditions) throws Exception
{
	DBResult result = null;
	try
	{
	    
		result = DBEngine.doSelect(con,"select * from " + TABLE_NAME,conditions);
		java.sql.ResultSet rs = result.getRs();
		Product obj = null;
		if(rs.next())
		{
			obj = fetch(rs);
		}
		return obj;
	}
	finally
	{
		if(result!=null) result.clear() ;
	}
}
public static Product fetch(java.sql.ResultSet rs) throws Exception
{
	Product obj = new Product();
	obj.initObject(rs);
	return obj;
}
protected void getUnique(DBConnection con) throws Exception
{
		setId(DBEngine.getId(con));
}
//----system generated end----//



//----add by chengbiao.he start----//
/*实现模糊查询*/

/*public List query(DBConnection con)throws Exception{
    List list = new ArrayList();
    
    String sql= "select * from T_PRODUCT  where NAME like %"+s_mhcx+"% or CONCENTRICITY_CODE like %+s_mhcx+%" +
    		"or MODEL like %+s_mhcx+% or SPEC like %+s_mhcx+% orPRODUCT_BRAND like%+s_mhcx+% or MANUFATURER like%+s_mhcx+% ";
    DBPreparedStatement dbps= con.prepareStatement(sql);
    ResultSet rs = dbps.executeQuery();
    list.add(rs);
     
    Iterator iterator = list.iterator();
    while(iterator.hasNext()){
        Product product =(Product)iterator.next();
        list.add(product);
    }
    return list;   
}
*/

public void canUpdateProduct(DBConnection con,String currWareCode) throws Exception
{
    /*if (true){//判断是否正式产品,是否非本仓库维护的临时产品
        throw new BizException("当前产品不可修改！");
    }*/
    
    if(ConstDefine.STR_YES.equals(getIsNormal())){
        throw new BizException("当前是正式产品不能修改");
    }
    if(!getWarehouseCode().equals(currWareCode)){
        throw new BizException("所选产品不属于本仓库产品不能修改");
    }
}

public String viewIsNormal(DBConnection con) throws Exception
{
    return SelectManager.viewYesOrNo(getIsNormal()+"");
}

public String viewStatus(DBConnection con) throws Exception
{
  return SelectManager.viewYesOrNo(getStatus()+"");   
}
public String viewIsBuy(DBConnection con) throws Exception
{
    return SelectManager.viewYesOrNo(getIsBuy()+"");
}
public void beforeUpdate(DBConnection con) throws Exception
{
    setLastUpdateTime(new DateUtil().getFullString());
}
public void beforeInsert(DBConnection con) throws Exception
{
    checkUnique(con);
    DateUtil du = new DateUtil();
    setCreateTime(du.getFullString());
    setLastUpdateTime(du.getFullString());
    setRecentAdjustedPrice(0);
} 

private void checkUnique(DBConnection con) throws Exception 
{
    Conditions cs = new Conditions();
    cs.add(CONCENTRICITY_CODE, getConcentricityCode());
    cs.add(ID, Conditions.NOT_EQUAL, getId());
    if(DBEngine.doCount(con, "select count(id) from "+TABLE_NAME, cs) > 0)
        throw new BizException("已存在的产品编码，请重新填写。");
}
/*
public List getConcentricityCode()
{ 
    List list = new ArrayList();
    Iterator iteractor = list.iterator();
    while(iteractor.hasNext()){
        
    }
    return m_concentricity_code;
}

public static Product fetchByCode(DBConnection con,String code) throws Exception
{
    Conditions conditions = new Conditions();

    conditions.add(ID,code);
    Product product = fetch(con,conditions);
    product.getConcentricityCode();
  
    return fetch(con,conditions);
} 
public void  setConcentricityCode(String v)
{  Product p = new Product();
 
p.getWarehouseCode()+p.getConcentricityCode().substring(6, 15).

   if(("0").equals(getIsNormal())){}
    if(getConcentricityCode().equals(v)) return ;
    
    m_concentricity_code = v ;
    markDirty(CONCENTRICITY_CODE);
}
*/

public String viewProviderCode(DBConnection con) throws Exception
{
    Conditions cs = new Conditions();
    cs.add(Provider.CODE,getProviderCode());
    Provider pro = Provider.fetch(con, cs);
    if (pro == null)
        return "";
    else
        return pro.viewName(con);
}



public static Product fetchByConcentricityCode(DBConnection con, String concentricityCode) throws Exception{
    Conditions conditions = new Conditions();
    conditions.add("upper(" + CONCENTRICITY_CODE + ")", concentricityCode.toUpperCase());
    return fetch(con,conditions);
}


public static Product fetchByApepCode(DBConnection con, String apepCode) throws Exception{
    Conditions conditions = new Conditions();
    conditions.add(APEP_CODE, apepCode);
    return fetch(con,conditions);
}

//----add by chengbiao.he end----//
//取得产品分类
public  String viewCategoryCode(DBConnection con)throws Exception{
   Conditions conditions = new Conditions();
   conditions.add(WarehouseCategory.CATEGORY_CODE,getCategoryCode());
   WarehouseCategory wc = WarehouseCategory.fetch(con, WarehouseCategory.CATEGORY_CODE);
   wc.getCategoryName();
    return wc.getCategoryName();
}

//----add by wing start----//
public String viewBalanceMode(DBConnection con) throws Exception
{
  return SelectManager.viewBalanceModel(getBalanceMode()+"");   
}

//----add by wing end----//

// add by Jackey start --//
public String viewWarehouseName(DBConnection con) throws Exception
{
    String mReturn = "";
    Warehouse w = Warehouse.fetchByCode(con, getWarehouseCode());  
    if (w != null)
    {
        mReturn = w.getName();
    }
    return  mReturn;
}

public String viewIsTradeflatProduct(DBConnection con) throws Exception
{
    String mReturn = "";
    if (getIsTradeflatProduct().equalsIgnoreCase(ConstDefine.STR_VAL_DEAL))
    {
        mReturn = ConstDefine.STR_NAME_DEAL;
    }
    else
    {
        mReturn = ConstDefine.STR_NAME_HAND;
    }
    return mReturn;
}

public String viewUnitPrice(DBConnection con) throws Exception
{
    return StringUtil.showPrice4(String.valueOf(getUnitPrice()));
}
// add by Jackey end

}

